﻿''==================================================
'' 文件：DeviceRestartTimeService.vb
'' 作者：李大龙
'' 日期：2011-1-06
'' 版本：v1.0
'' 描述：从设备中读取设备启动和停止时间数据


''' <summary>
''' 设备端点重启时间
''' </summary>
''' <remarks></remarks>
Public Class DevicePowerRestartTime
    '断电现场数据页地址
    Private Shared _pageAddress As Short = 3750

    '断电现场每条记录的长度
    Private Shared ReadOnly _RecordLength As Integer = 12
    '历史记录断电时间记录指针位置
    Private Shared ReadOnly _RecordIndex As Integer = 504


    ''' <summary>
    ''' 读取指定设备的断电现场数据（1页505字节的数据）
    ''' </summary>
    ''' <param name="netID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function ReadPowerTime(ByVal netID As Byte) As Byte()
        Dim buf(510) As Byte

        '读取前半页
        Dim b1() As Byte
        b1 = COMHelper.Instance.Send4Byte(netID, FunctionCode.HistoryByteRead, _pageAddress, 1)

        If b1 Is Nothing Then
            Return Nothing
        End If
        Array.Copy(b1, 0, buf, 0, 255)

        '读取后半页
        Dim b2() As Byte
        b2 = COMHelper.Instance.Send4Byte(netID, FunctionCode.HistoryByteRead, _pageAddress, 2)
        If b2 Is Nothing Then
            Return Nothing
        End If
        Array.Copy(b2, 0, buf, 255, 255)

        Return buf
    End Function


    Public Shared Function GetPowerTimeList(ByVal netID As Byte) As IList(Of DeviceRestartTime)

        Dim lst As IList(Of DeviceRestartTime) = New List(Of DeviceRestartTime)

        Dim buf() As Byte
        '读取页数据
        buf = ReadPowerTime(netID)

        If (buf Is Nothing) Then
            Return lst
        End If

        '断电前1小时年
        Dim iYear As Integer

        '断电前1小时月
        Dim iMonth As Integer

        '断电前1小时日
        Dim iDay As Integer

        '断电前1小时小时
        Dim iHour As Integer

        Dim iMinute As Integer
        Dim iSecond As Integer

        Dim iRunYear As Integer
        Dim iRunMonth As Integer
        '重启时间－日
        Dim iRunDay As Integer

        '重启时间－小时
        Dim iRunHour As Integer

        Dim iRunMinute As Integer
        Dim iRunSecond As Integer

        '当前记录时间的位置索引
        '   Dim curIndex As Integer
        '  curIndex = buf(504) '共40个10字节的记录后面的值

        '该页保存最近40个断电现场数据
        For i As Integer = 0 To HIST_LOSTPOWER_RECCOUNT_NEW - 1

            iYear = buf(i * _RecordLength + 0)
            iMonth = buf(i * _RecordLength + 1)
            iDay = buf(i * _RecordLength + 2)
            iHour = buf(i * _RecordLength + 3)
            iMinute = buf(i * _RecordLength + 4)
            iSecond = buf(i * _RecordLength + 5)

            iRunYear = buf(i * _RecordLength + 6)
            iRunMonth = buf(i * _RecordLength + 7)
            iRunDay = buf(i * _RecordLength + 8)
            iRunHour = buf(i * _RecordLength + 9)
            iRunMinute = buf(i * _RecordLength + 10)
            iRunSecond = buf(i * _RecordLength + 11)

            If iRunDay = 0 Then
                '该位置为初始化数据0，没有有效数据。
                Continue For
            End If

            Dim item As New DeviceRestartTime()
            item.NetID = netID

            '断电时间
            If i = 0 Then '第一条上电记录(只包括上电信息)
                item.IsNew = 1
                item.LastTime = New DateTime(1900, 1, 1)
            Else
                item.LastTime = New DateTime(1900 + iYear, iMonth, iDay, iHour, iMinute, iSecond)
                item.IsNew = 0
            End If
            item.RestartedTime = New DateTime(1900 + iRunYear, iRunMonth, iRunDay, iRunHour, iRunMinute, iRunSecond) '第一条断电记录增加保存了年和月数据

            lst.Add(item)
        Next

        Return lst
    End Function

End Class
